//
//  ConfirmPhoneNumViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/14.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "ConfirmPhoneNumViewController.h"
#import "AddBandCardSuccessViewController.h"
#import "BandCardStore.h"
#import "BankCarList.h"
#import "BandCardAddParametersModel.h"

@interface ConfirmPhoneNumViewController ()
{
    dispatch_source_t timer;
    __weak IBOutlet UIButton *SubTime;
    __weak IBOutlet UITextField *codeTf;
    __weak IBOutlet UILabel *tsLa;
    
}

@property (nonatomic,copy) NSString *reqCode;
@property (nonatomic, assign) NSInteger lesstime;

@end

@implementation ConfirmPhoneNumViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lesstime = 60;
    [self setNavBarWithTitle:@"验证手机号"];
    
    codeTf.keyboardType = UIKeyboardTypeNumberPad;
    
    tsLa.text = [NSString stringWithFormat:@"本次操作需要短信确认，请输入%@*****%@收到的短信校验码",[self.phoneNum substringToIndex:3],[self.phoneNum substringFromIndex:8]];
    
    [self getCode];
    
    [self setTimer];
}

#pragma mark - 获取验证码
- (void)getCode{
    
    BandCardStore *store = [[BandCardStore alloc] init];
    
    MJWeakSelf
    [store getCodeWithTelephoneNumber:self.phoneNum Success:^(NSString *code) {
        
        weakSelf.reqCode = code;
        
    } Failure:^(NSError *error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

- (IBAction)sendCode:(UIButton *)sender {
    [self getCode];
    [self setTimer];
}
- (void)setTimer {
    __block int timeout = (int)_lesstime; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self->SubTime setTitle:@"| 获取验证码" forState:UIControlStateNormal];
                self->SubTime.enabled = YES;
            });
        }else{
            NSString *strTime;
            if (timeout == 60) {
                strTime = [NSString stringWithFormat:@"%ld",self.lesstime];
            }else {
                int seconds = timeout % self.lesstime;
                strTime = [NSString stringWithFormat:@"%.2d", seconds];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self->SubTime setTitle:[NSString stringWithFormat:@"| %@秒后重发",strTime] forState:UIControlStateNormal];
                self->SubTime.enabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (void)viewWillDisappear:(BOOL)animated{

    timer = nil;
    
}

#pragma mark - 下一步
- (IBAction)onNextBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if (codeTf.text.length == 0) {

        return [self showMBPError:@"填写验证码"];
    }

    if (![codeTf.text isEqualToString:self.reqCode]) {

        return [self showMBPError:@"请输入正确验证码"];
    }
    
    BandCardAddParametersModel *model = [BankCarList sharedInstance].addParametersModel;
    
    MJWeakSelf
    BandCardStore *store = [[BandCardStore alloc] init];
    [store addBandCardListWithCustomId:[UserDefaultsManager sharedInstance].user_id banNum:model.cardNumber bankName:model.bankName andPhone:model.phone andType:model.type Success:^{
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addSuccess" object:nil];
        
        AddBandCardSuccessViewController *VC = [[AddBandCardSuccessViewController alloc] init];
        
        [self.navigationController pushViewController:VC animated:YES];
        
    } Failure:^(NSError *error) {
       
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
    
}

@end
